<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>rational - rake: Ruby Standard Library Documentation</title>


<script src="../../js/navigation.js" defer></script>
<script src="../../js/search.js" defer></script>
<script src="../../js/search_index.js" defer></script>
<script src="../../js/searcher.js" defer></script>
<script src="../../js/darkfish.js" defer></script>

<script src="../../js/jquery-3.2.0.min.js"></script>

<script src="../../js/vue.min.js"></script>
<script src="../../js/js.cookie.min.js"></script>

<link href="../../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../../css/rdoc.css" rel="stylesheet">
<link href="../../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../../";
  var index_rel_prefix = "../../";
  var darkModeCsseHref = "../../css/rdoc-dm.css"
  var defaultModeCssHref = "../../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="file">
  <!-- this is page.html -->
  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->


 <div class='wrapper hdiv'>

 


<nav id='vapp' role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../../table_of_contents.html#pages">Pages</a>
    <a href="../../table_of_contents.html#classes">Classes</a>
    <a href="../../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
  <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>

  <div id="project-metadata">
   <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
    
<div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
    <li><a href="../../rake-13_0_6/History_rdoc.html">History</a>
    <li><a href="../../rake-13_0_6/README_rdoc.html">README</a>
    <li><a href="../../rake-13_0_6/doc/command_line_usage_rdoc.html">command_line_usage</a>
    <li><a href="../../rake-13_0_6/doc/glossary_rdoc.html">glossary</a>
    <li><a href="../../rake-13_0_6/doc/proto_rake_rdoc.html">proto_rake</a>
    <li><a href="../../rake-13_0_6/doc/rakefile_rdoc.html">rakefile</a>
    <li><a href="../../rake-13_0_6/doc/rational_rdoc.html">rational</a>
  </ul>
</div>

   </div>
  </div>
</nav>


<!--  carbon ads here -->

<div id='extraz'>
  <div class='adzbox-index'  >
   
  </div>
 </div>         


<main role="main" aria-label="Page rake-13.0.6/doc/rational.rdoc">

<h1 id="label-Why+rake-3F">Why rake?<span><a href="#label-Why+rake-3F">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>Ok, let me state from the beginning that I never intended to write this code.  I’m not convinced it is useful, and I’m not convinced anyone would even be interested in it.  All I can say is that Why’s onion truck must by been passing through the Ohio valley.</p>

<p>What am I talking about? … A Ruby version of Make.</p>

<p>See, I can sense you cringing already, and I agree.  The world certainly doesn’t need yet another reworking of the “make” program.  I mean, we already have “ant”.  Isn’t that enough?</p>

<p>It started yesterday.  I was helping a coworker fix a problem in one of the Makefiles we use in our project.  Not a particularly tough problem, but during the course of the conversation I began lamenting some of the shortcomings of make.  In particular, in one of my makefiles I wanted to determine the name of a file dynamically and had to resort to some simple scripting (in Ruby) to make it work.  “Wouldn’t it be nice if you could just use Ruby inside a Makefile” I said.</p>

<p>My coworker (a recent convert to Ruby) agreed, but wondered what it would look like.  So I sketched the following on the whiteboard…</p>

<pre>&quot;What if you could specify the make tasks in Ruby, like this ...&quot;

  task &quot;build&quot; do
    java_compile(...args, etc ...)
  end

&quot;The task function would register &quot;build&quot; as a target to be made,
and the block would be the action executed whenever the build
system determined that it was time to do the build target.&quot;</pre>

<p>We agreed that would be cool, but writing make from scratch would be WAY too much work.  And that was the end of that!</p>

<p>… Except I couldn’t get the thought out of my head.  What exactly would be needed to make the about syntax work as a make file?  Hmmm, you would need to register the tasks, you need some way of specifying dependencies between tasks, and some way of kicking off the process. Hey!  What if we did … and fifteen minutes later I had a working prototype of Ruby make, complete with dependencies and actions.</p>

<p>I showed the code to my coworker and we had a good laugh.  It was just about a page worth of code that reproduced an amazing amount of the functionality of make.  We were both truly stunned with the power of Ruby.</p>

<p>But it didn’t do everything make did.  In particular, it didn’t have timestamp based file dependencies (where a file is rebuilt if any of its prerequisite files have a later timestamp).  Obviously THAT would be a pain to add and so Ruby Make would remain an interesting experiment.</p>

<p>… Except as I walked back to my desk, I started thinking about what file based dependencies would really need.  Rats!  I was hooked again, and by adding a new class and two new methods, file/timestamp dependencies were implemented.</p>

<p>Ok, now I was really hooked.  Last night (during CSI!) I massaged the code and cleaned it up a bit.  The result is a bare-bones replacement for make in exactly 100 lines of code.</p>

<p>For the curious, you can see it at …</p>
<ul><li>
<p>doc/proto_rake.rdoc</p>
</li></ul>

<p>Oh, about the name.  When I wrote the example Ruby Make task on my whiteboard, my coworker exclaimed “Oh! I have the perfect name: Rake … Get it?  Ruby-Make. Rake!”  He said he envisioned the tasks as leaves and Rake would clean them up  … or something like that.  Anyways, the name stuck.</p>

<p>Some quick examples …</p>

<p>A simple task to delete backup files …</p>

<pre class="ruby"><span class="ruby-identifier">task</span> <span class="ruby-value">:clean</span> <span class="ruby-keyword">do</span>
  <span class="ruby-constant">Dir</span>[<span class="ruby-string">&#39;*~&#39;</span>].<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">fn</span><span class="ruby-operator">|</span> <span class="ruby-identifier">rm</span> <span class="ruby-identifier">fn</span> <span class="ruby-keyword">rescue</span> <span class="ruby-keyword">nil</span>}
<span class="ruby-keyword">end</span>
</pre>

<p>Note that task names are symbols (they are slightly easier to type than quoted strings … but you may use quoted string if you would rather). Rake makes the methods of the <a href="../../FileUtils.html"><code>FileUtils</code></a> module directly available, so we take advantage of the <code>rm</code> command.  Also note the use of “rescue nil” to trap and ignore errors in the <code>rm</code> command.</p>

<p>To run it, just type “rake clean”.  Rake will automatically find a Rakefile in the current directory (or above!) and will invoke the targets named on the command line.  If there are no targets explicitly named, rake will invoke the task “default”.</p>

<p>Here’s another task with dependencies …</p>

<pre class="ruby"><span class="ruby-identifier">task</span> <span class="ruby-value">:clobber</span> <span class="ruby-operator">=&gt;</span> [<span class="ruby-value">:clean</span>] <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">rm_r</span> <span class="ruby-string">&quot;tempdir&quot;</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Task :clobber depends upon task :clean, so :clean will be run before :clobber is executed.</p>

<p>Files are specified by using the “file” command.  It is similar to the task command, except that the task name represents a file, and the task will be run only if the file doesn’t exist, or if its modification time is earlier than any of its prerequisites.</p>

<p>Here is a file based dependency that will compile “hello.cc” to “hello.o”.</p>

<pre class="ruby"><span class="ruby-identifier">file</span> <span class="ruby-string">&quot;hello.cc&quot;</span>
<span class="ruby-identifier">file</span> <span class="ruby-string">&quot;hello.o&quot;</span> <span class="ruby-operator">=&gt;</span> [<span class="ruby-string">&quot;hello.cc&quot;</span>] <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">t</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">srcfile</span> = <span class="ruby-identifier">t</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-regexp">/\.o$/</span>, <span class="ruby-string">&quot;.cc&quot;</span>)
  <span class="ruby-identifier">sh</span> <span class="ruby-node">%{g++ #{srcfile} -c -o #{t.name}}</span>
<span class="ruby-keyword">end</span>
</pre>

<p>I normally specify file tasks with string (rather than symbols).  Some file names can’t be represented by symbols.  Plus it makes the distinction between them more clear to the casual reader.</p>

<p>Currently writing a task for each and every file in the project would be tedious at best.  I envision a set of libraries to make this job easier.  For instance, perhaps something like this …</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;rake/ctools&#39;</span>
<span class="ruby-constant">Dir</span>[<span class="ruby-string">&#39;*.c&#39;</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">fn</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">c_source_file</span>(<span class="ruby-identifier">fn</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>where “c_source_file” will create all the tasks need to compile all the C source files in a directory.  Any number of useful libraries could be created for rake.</p>

<p>That’s it.  There’s no documentation (other than whats in this message).  Does this sound interesting to anyone?  If so, I’ll continue to clean it up and write it up and publish it on RAA.  Otherwise, I’ll leave it as an interesting exercise and a tribute to the power of Ruby.</p>

<p>Why /might/ rake be interesting to Ruby programmers.  I don’t know, perhaps …</p>
<ul><li>
<p>No weird make syntax (only weird Ruby syntax :-)</p>
</li><li>
<p>No need to edit or read XML (a la ant)</p>
</li><li>
<p>Platform independent build scripts.</p>
</li><li>
<p>Will run anywhere Ruby exists, so no need to have “make” installed. If you stay away from the “sys” command and use things like ‘ftools’, you can have a perfectly platform independent build script.  Also rake is only 100 lines of code, so it can easily be packaged along with the rest of your code.</p>
</li></ul>

<p>So … Sorry for the long rambling message.  Like I said, I never intended to write this code at all.</p>

</main>

</div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

